package com.hb.trade.server.center.au.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hb.core.pojo.center.au.dto.UserExtDTO;
import com.hb.core.pojo.center.au.mapper.UserExtMapper;
import com.hb.core.pojo.center.au.entity.UserExt;
import com.hb.trade.server.center.au.service.UserExtService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 用户扩展信息表 服务实现类
 * </p>
 *
 * @author zhaojk
 * @since 2021-11-29
 */
@Service
public class UserExtServiceImpl extends ServiceImpl<UserExtMapper, UserExt> implements UserExtService {

    /**
     * 更新用户的扩展信息
     *
     * @param userExt
     * @return
     */
    @Override
    public boolean updateByUserId(UserExtDTO userExt) {
        Long userId = userExt.getUserId();
        if (userId == null) {
            return false;
        }
        QueryWrapper<UserExt> wrapper = UserExt.wrapper();
        wrapper.eq("user_id", userId);
        UserExt one = getOne(wrapper);
        if (one == null) {
            userExt.buildBaseData(1);
            return save(userExt);
        }

        userExt.to(one);
        return updateById(one);
    }
    /**
     *
     * @Description: 根据用户id查询用户扩展信息
     * @Author: zhongdl
     * @Date:2022/2/14 8:51
     * @param userId:
     * @return: {@link UserExt }
     **/
    @Override
    public UserExt getUserExtByUserId(Long userId) {
        if (userId == null){
            return null;
        }
        QueryWrapper<UserExt> wrapper = UserExt.wrapper();
        wrapper.eq("user_id",userId).last("limit 1");
        return getOne(wrapper);
    }
}
